package com.hc.demo;

import com.github.houbb.data.factory.core.util.DataUtil;
import com.hc.res.CategoryItem;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


/**
 * @author hecai
 * @date 2021/12/6 17:54
 */
public class NewFeatureDemo {

    /**
     * sorted：对流中的元素进行排序
     */
    @Test
    public void fun5() {
        List<String> list = List.of("peter", "anna", "mike", "xenia", "grenn");
        list.stream()
                .sorted((item1, item2) -> item1.compareTo(item2))
                .sorted(String::compareTo)
                .forEach(System.out::println);
    }

    /**
     * map：对流中的元素进行转换
     */
    @Test
    public void fun4() {
        List<String> list = List.of("peter", "anna", "mike", "xenia", "grenn");
        list.stream().map(String::toUpperCase).forEach(System.out::println);
    }

    /**
     * peek：将一个流转换为另一个流
     */
    @Test
    public void fun3() {
        List<String> list = List.of("peter", "anna", "mike", "xenia");
        list.stream().peek(item -> System.out.println("value: " + item)).forEach(System.out::println);
    }

    /**
     * filter过滤：参考是Predicate（断言）
     */
    @Test
    public void fun2() {
        List<String> list = List.of("aaa", "bbbb", "cccccc", "dd", "ee", "fff");
        List<String> res = list.stream().filter(item -> item.length() > 3).collect(Collectors.toList());
        res.forEach(System.out::println);
    }

    /**
     * map：对集合中的元素进行转换
     * 将列表中的每一个ProductCategoryItem的Id抽取出来组成一个新的集合
     */
    @Test
    public void fun1() {
        List<CategoryItem> list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            CategoryItem categoryItem = DataUtil.build(CategoryItem.class);
            list.add(categoryItem);
        }
        List<Integer> res = list.stream()
                .map(item -> item.getId())
                .collect(Collectors.toList());
        res.forEach(System.out::println);
    }
}
